package com.dcd.controller;

import com.dcd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*加载多个数据封装成map*/
@RestController
@RequestMapping("/PublicController")
public class PublicController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private EnergyService energyService;
    @Autowired
    private ProgramService programService;
    @Autowired
    private Car_modelService car_modelService;
    @Autowired
    private Car_levelService car_levelService;
    @Autowired
    private Car_seriesService car_seriesService;
    @Autowired
    private Video_typeService video_typeService;
    @Autowired
    private Different_countriesService different_countriesService;

    /**
     * 选车页面加载全部数据
     *
     * @param
     * @return
     */
    @RequestMapping("/load_all_vehicle_selection_data")
    public Map<String, Object> load_all_vehicle_selection_data() {
        HashMap<String, Object> map = new HashMap<>();
        /*加载所有级别*/
        map.put("all_car_level", car_levelService.select_all_level());
        /*加载全部能源*/
        map.put("all_energy", energyService.select_all_energy());
        /*加载全部国别*/
        map.put("all_different_countries", different_countriesService.select_all_different_countries());
        return map;
    }

    /**
     * 加载车系首页全部数据
     *
     * @param car_series_id 车系id
     * @return
     */
    @RequestMapping("/load_all_car_series_index_data/{car_series_id}")
    public Map<String, Object> load_all_car_series_index_data(@PathVariable int car_series_id, HttpServletResponse response, @CookieValue(name = "history_browsing_car_series_ids", required = false) Cookie history_browsing_car_series_ids) {
        HashMap<String, Object> map = new HashMap<>();
        /*加载车系信息*/
        map.put("car_series", car_seriesService.select_car_series_by_id(car_series_id));
        /*加载给车系全部车型*/
        map.put("all_car_model", car_modelService.modelList(car_series_id));
        /*保存历史浏览车系id*/
        save_history_browsing_car_series_ids(response, history_browsing_car_series_ids, car_series_id);
        return map;
    }

    /**
     * 保存历史浏览车系id
     *
     * @param response
     * @param history_browsing_car_series_ids 历史浏览id
     * @param car_series_id                   查询id
     */
    private void save_history_browsing_car_series_ids(HttpServletResponse response, Cookie history_browsing_car_series_ids, int car_series_id) {
        StringBuffer new_all_ids = new StringBuffer();//保存新的ids
        if (history_browsing_car_series_ids == null || history_browsing_car_series_ids.equals("")) {
            new_all_ids.append(car_series_id);
        } else {
            String all_ids = history_browsing_car_series_ids.getValue();
            String[] all_ids_array = all_ids.split("-");//11-2-32-4
            List<String> all_ids_condition = new ArrayList<>();
            for (int i = 0; i < all_ids_array.length; i++) {
                all_ids_condition.add(all_ids_array[i]);
            }
            all_ids_condition.remove(String.valueOf(car_series_id));//删除已存在的id
            List<Integer> new_all_ids_condition = new ArrayList<>();
            new_all_ids_condition.add(car_series_id);
            for (String id : all_ids_condition) {
                new_all_ids_condition.add(Integer.parseInt(id));
            }
            for (int i = 0; i < new_all_ids_condition.size(); i++) {
                new_all_ids.append(new_all_ids_condition.get(i));
                if (i == 9)
                    break;
                if (i != new_all_ids_condition.size() - 1) {
                    new_all_ids.append("-");
                }
            }
        }
        Cookie new_cookie = new Cookie("history_browsing_car_series_ids", new_all_ids.toString());
        new_cookie.setMaxAge(600);/*600s*/
        new_cookie.setPath("/");
        response.addCookie(new_cookie);
    }

    /**
     * 加载视频页面全部数据
     *
     * @return
     */
    @RequestMapping("/load_all_car_video_data")
    public Map<String, Object> load_all_car_video_data() {
        HashMap<String, Object> map = new HashMap<>();
        /*加载所有视频类型*/
        map.put("all_video_type", video_typeService.select_All_Video_type());
        /* 查询最新的视频信息*/
        map.put("all_new_video_mess", videoService.select_new_video_mess());
        return map;
    }

    /**
     * 加载指定节目页面全部信息
     *
     * @param program_id
     * @return
     */
    @RequestMapping("/load_all_program_data/{program_id}")
    public Map<String, Object> select_appoint_program_by_id(@PathVariable int program_id) {
        HashMap<String, Object> map = new HashMap<>();
        /*查询指定节目信息*/
        map.put("all_program_data", programService.select_appoint_program_by_id(program_id));
        /*查询指定节目下所有视频的所有车系*/
        map.put("all_car_series_data", car_seriesService.select_appoint_program_all_car_series(program_id));
        return map;
    }
}
